﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Glissando
{
    /// <summary>
    /// Interaction logic for StaffControl.xaml
    /// </summary>
    public partial class StaffControl : UserControl
    {
        public bool Treble { get; set; }

        public StaffControl()
        {
            InitializeComponent();
            this.SizeChanged += new SizeChangedEventHandler(StaffControl_SizeChanged);
        }

        void StaffControl_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            CreateStaff();
        }

        double GetY(double line)
        {
            return ActualHeight * (line * 0.5/4 + 0.25);
        }

        private void CreateStaff()
        {
            StaffCanvas.Children.Clear();
            if (ActualHeight == 0) return;

            for (int i = 0; i < 5; i++)
            {
                Line staffLine = new Line();
                staffLine.X1 = 0;
                staffLine.Y1 = GetY(i);
                staffLine.X2 = ActualWidth;
                staffLine.Y2 = staffLine.Y1;
                staffLine.StrokeThickness = ActualHeight*0.01;
                staffLine.Stroke = Foreground;
                staffLine.SnapsToDevicePixels = true;
                StaffCanvas.Children.Add(staffLine);
            }

            // http://en.wikipedia.org/wiki/Clef

            TextBlock clef = new TextBlock() { Text = ":", FontFamily = new FontFamily("MetDemo"), FontSize = ActualHeight*0.52 };
            clef.Text = Treble ? ":" : "$"; // %%%
            clef.Foreground = this.Foreground;
            Canvas.SetLeft(clef, 10);
            Canvas.SetBottom(clef, GetY(-1.75));
            clef.VerticalAlignment = VerticalAlignment.Bottom;
            StaffCanvas.Children.Add(clef);
        }
    }
}
